<?php
/**
 * Created by PhpStorm.
 * User: dancheng
 * Date: 2018/1/19
 * Time: 9:48
 */
class User_model extends CI_Model{
    /**
     * User_model constructor.
     * 构造函数，加载数据库配置文件
     */
    public function __construct() {
        $this->load->database();
    }

    /**
     * @return mixed
     * 查询全部人员信息
     */
    public function get_All(){
        $query = $this->db->get('user');
        return $query->result_array();
    }

    /**
     * @param $data
     * 添加人员
     */
    public function add_user($data){
        $this->db->insert('user', $data);
    }

    /**
     * @param $id
     * 删除人员
     */
    public function delete_user($id){
        $this->db->delete('user', array('user_id' => $id));
    }

    /**
     * @param $data
     * 修改信息
     */
    public function update_user($data){
        $this->db->update('user', $data, array('user_id' => $data['user_id']));
    }

    /**
     * @param $id
     * @return mixed
     * 按id查询
     */
    public function getUserById($id){
        $query = $this->db->get_where('user', array('user_id' => $id));
        return $query->row_array();
    }

    /**
     * @param $username
     * @param $password
     * @return mixed
     * 登录验证
     */
    public function check($username,$password){
        $query =$this->db->get_where('user',array('username'=>$username,'password'=>$password));
        return $query->row_array();
    }
    /**
     * @param $username
     * @return mixed
     * 按姓名查找
     */
    public function findByName($username){
        $query =$this->db->get_where('user',array('username'=>$username));
        return $query->row_array();
    }

    /**
     * @param $data
     * @return mixed
     * 模糊查询.
     */
    public function findlike($data){
        $query=$this->db->get_where('user',"username like '%$data%'");
        return $query->result_array();
    }

    /**
     * @param $id
     * @return mixed
     * 得到用户菜单
     */
    public function get_menu($id){
        $sql ="SELECT DISTINCT c.menu_id id,c.parent_id parentid,c.name title,c.icon,c.url href FROM  ci_user_role a INNER JOIN ci_role_menu b ON a.role_id = b.role_id INNER JOIN ci_menu c ON c.menu_id = b.menu_id WHERE user_id = ?";
        $query=$this->db->query($sql, array($id));;
        return $query->result_array();
    }
}